package com.patchmanage.back.utils;

/**
 * @className: PingUtil
 * @description:
 * @author: huizhao
 * @date: 2021/4/17
 **/

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class PingUtil {

    private static final Log log = LogFactory.get();

    public static String ping(String url) {
        StringBuffer buf = new StringBuffer();
        String s = "";
        Process process;
        try {
            String osName = System.getProperty("os.name");//获取操作系统类型
            String command = "";
            log.info("================================" + osName + "===============================================");
            if (osName.contains("Linux")) {
                command = "ping -c 100 -i 0 " + url;
            } else if(osName.contains("window")) {
                command = "ping -n 5 -w 1000 " + url;
            }

            process = Runtime.getRuntime().exec(command);
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    process.getInputStream(), "gbk"));
            while ((s = br.readLine()) != null) {
                buf.append(s + "\r\n");
            }

            process.waitFor();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return buf.toString();
    }
}